package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.MessageConstant;
import com.atguigu.dao.MemberDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSrevice;
import com.atguigu.util.DateUtils;
import com.atguigu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = OrderSrevice.class)
@Transactional
public class OrderSreviceImpl implements OrderSrevice {

    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderSettingDao orderSettingDao;

    @Autowired
    MemberDao memberDao;

    @Override
    public Result saveOrder(Map map) throws Exception {
        String orderDateStr = (String)map.get("orderDate");
        String telephone = (String)map.get("telephone");
        String name = (String)map.get("name");
        String sex = (String)map.get("sex");
        String idCard = (String)map.get("idCard");
        String setmealIdStr = (String)map.get("setmealId");
        Integer setmealId = Integer.parseInt(setmealIdStr);
        Date orderDate = DateUtils.parseString2Date(orderDateStr);

        //1.预约设置不存在，或可预约人数是0，无法预约。
        OrderSetting orderSetting = orderSettingDao.findByOrderData(orderDate);
        if(orderSetting==null|| orderSetting.getNumber()==0){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }else{
            //2.如果已预约人数大于等于可预约人数，无法预约。
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            if(reservations>=number){
                return new Result(false,MessageConstant.ORDER_FULL);
            }
        }

        //3.查询会员，会员不存在进行快速注册。会员存在可以预约
        Member member = memberDao.getMemberByTelephone(telephone);
        if(member==null){
            member = new Member();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberDao.save(member); //主键回填
        }else{
            //4.判断该日期是否重复预约，约过则不能再约。
            Map mapParam = new HashMap();
            mapParam.put("memberId",member.getId());
            mapParam.put("orderDate",orderDate);
            mapParam.put("setmealId",setmealId);

            int count = orderDao.findByCondition(mapParam);
            if(count>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        //5.下订单 并 已预约人数+1
        Order orderObj = new Order();
        orderObj.setMemberId(member.getId());
        orderObj.setOrderDate(orderDate);
        orderObj.setOrderType(Order.ORDERTYPE_WEIXIN);
        orderObj.setOrderStatus(Order.ORDERSTATUS_NO);
        orderObj.setSetmealId(setmealId);
        orderDao.save(orderObj); //主键回填

        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.updateReservations(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,orderObj);
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById(id);
        Date orderDate = (Date) map.get("orderDate");
        map.put("orderDate",DateUtils.parseDate2String(orderDate));
        return map;
    }
}
